//
//  SCEmoticonCollection.swift
//  weibo
//
//  Created by 阿城 on 15/12/15.
//  Copyright © 2015年 阿城. All rights reserved.
//

import UIKit

let kEmoticonNotification = "kEmoticonNotification"
let kDeleteNotification = "kDeleteNotification"

class SCEmoticonCollection: UICollectionView {

    var emoticonCollectionBackclosure: ((indexpath: NSIndexPath)->())?
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        //配置layout
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        layout.scrollDirection = .Horizontal
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
        //配置collectionView
        clipsToBounds = false
        pagingEnabled = true
        bounces = false
        dataSource = self
        delegate = self
        showsHorizontalScrollIndicator = false
        
        registerClass(SCEmoticonCell.self, forCellWithReuseIdentifier: "cell")
        
    }
    
    //有没有别的只设置一次的方法
    override func layoutSubviews() {
        super.layoutSubviews()
//        printLog(frame)
        
        //cell全屏
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = frame.size
    }
    
    
}

extension SCEmoticonCollection: UICollectionViewDataSource, UICollectionViewDelegate{
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return SCEmoticonViewModel.shareEmoticons.contentEmoticon.count
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return SCEmoticonViewModel.shareEmoticons.contentEmoticon[section].count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! SCEmoticonCell
//        cell.backgroundColor = SCColor.getRadomColor()
        cell.lab.text = "\(indexPath.section)--\(indexPath.item)"
        
        cell.dataList = SCEmoticonViewModel.shareEmoticons.contentEmoticon[indexPath.section][indexPath.row]
        cell.resentLab.hidden = indexPath.section != 0
        
        return cell
    }
    
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        let visibles = visibleCells()
        
//        printLog(visibles.count)
        if visibles.count != 1{

            //剩余量
            let first = abs(Int32(visibles[0].x - contentOffset.x))
            let second = abs(Int32(visibles[1].x - contentOffset.x))
            
            let cell = first < second ? visibles[0] : visibles[1]
            
            let idx = indexPathForCell(cell)
            
//            printLog("\(idx!)\(CGRectGetMaxX(first.frame) - contentOffset.x)")
            //闭包回调
            emoticonCollectionBackclosure?(indexpath: idx!)
        }
        
    }
    
    
}





